package com.example.goodtrash.login;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.goodtrash.MainActivity;
import com.example.goodtrash.R;

public class login extends AppCompatActivity {
   SharedPreferences sp;
    private EditText et_name;
    private EditText et_pwd;
    private CheckBox cb_remeberpwd;
    private CheckBox cb_autologin;
    private Button bt_register;
    private Button bt_login;
    private String userName, password, spPsw;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_GoodTrash);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        sp=getSharedPreferences("config", Context.MODE_PRIVATE);
        initView();
        boolean rememberpwd=sp.getBoolean("remeberpwd",false);
        boolean autologin=sp.getBoolean("autologin",false);
        if (rememberpwd){
            String pwd=sp.getString("pwd",null);
            et_pwd.setText(pwd);
            String name=sp.getString("name",null);
            et_name.setText(name);
            cb_remeberpwd.setChecked(true);
        }
        if(!"back".equals(getIntent().getStringExtra("state"))){
        if(autologin){
            cb_autologin.setChecked(true);
            String pwd=sp.getString("pwd",null);
            et_pwd.setText(pwd);
            String name =sp.getString("name",null);
            et_name.setText(name);
            startActivity(new Intent(login.this, MainActivity.class));
            finish();
        }
        } else {
            SharedPreferences.Editor editor=sp.edit();
            editor.putBoolean("autologin",false);
            editor.commit();
        }
    }

    private void initView(){
        et_name = findViewById(R.id.et_name);
        et_pwd = findViewById(R.id.et_pwd);
        cb_remeberpwd = findViewById(R.id.cb_remeberpwd);
        cb_autologin = findViewById(R.id.cb_autologin);
        bt_register = findViewById(R.id.bt_register);
        bt_login = findViewById(R.id.bt_login);
        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(login.this, Register.class));
            }
        });
        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name=et_name.getText().toString().trim();
                String pwd=et_pwd.getText().toString().trim();
                if (TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
                    Toast.makeText(login.this,"用户名或者密码为空",Toast.LENGTH_SHORT).show();
                }else {
                    String md5Psw= MD5Utils.md5(pwd);
                    spPsw=readPsw(name);
                    if(md5Psw.equals(spPsw)){
                        if (cb_remeberpwd.isChecked()) {
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("name", name);
                            editor.putString("pwd", pwd);
                            editor.putBoolean("remeberpwd", true);
                            editor.apply();
                        }else {
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putBoolean("remeberpwd", false);
                            editor.commit();
                        }

                        if (cb_autologin.isChecked()){
                            SharedPreferences.Editor editor=sp.edit();
                            editor.putBoolean("autologin",true);
                            editor.apply();
                        } else {
                            SharedPreferences.Editor editor=sp.edit();
                            editor.putBoolean("autologin",false);
                            editor.commit();
                        }
                        Toast.makeText(login.this,"登陆成功",Toast.LENGTH_SHORT).show();
                        saveLoginStatus(true,name);
                        Intent data=new Intent();
                        data.putExtra("isLogin",true);
                        setResult(RESULT_OK,data);
                        login.this.finish();
                        startActivity(new Intent(login.this,MainActivity.class));
                        finish();
                        return;
                    }else {
                        Toast.makeText(login.this,"输入用户名和密码有误",Toast.LENGTH_SHORT).show();

                    }
                }
            }
        });
    }
    private void saveLoginStatus(boolean status,String name){
        SharedPreferences sp=getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putBoolean("isLogin",status);
        editor.putString("loginUserName",name);
        editor.commit();
    }

    private String readPsw(String name) {
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        return sp.getString(name, "");
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {


        if(data!=null){
            String name =data.getStringExtra("name");
            if(!TextUtils.isEmpty(name)){
                et_name.setText(name);
                et_name.setSelection(name.length());
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}